package com.wyp168.renrenfilm.core.controller;

import java.util.*;

import com.github.pagehelper.PageInfo;
import com.wyp168.renrenfilm.core.service.RequireService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wyp168.renrenfilm.core.entity.FilmEntity;
import com.wyp168.renrenfilm.core.service.FilmService;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 电影表
 *
 * @author wyp168
 * @email 2721255249@qq.com
 * @date 2022-11-16 18:36:43
 */
@RestController
@Slf4j
@RequestMapping("film/film")
public class FilmController {
    @Autowired
    private FilmService filmService;

    @Autowired
    private RequireService requireService;

    Map<String, String> map = new HashMap();

    /**
     * 列表
     */
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public R list(@RequestParam  Map<String, Object> params){
//        PageInfo<FilmEntity> filmEntityPageInfo = filmService.recommonFilm(params);
        return R.ok().put("page", filmService.recommonFilm(params));
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		FilmEntity film = filmService.getByIdAndAddHotPoint(id);

        return R.ok().put("film", film);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(HttpServletRequest request, @RequestBody FilmEntity film) {
        film.setResourceSn(UUID.randomUUID().toString());
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return R.error("token为空");
        }

        String username = requireService.getLoginUser(token);


        film.setCreatedBy(username);
        film.setModifiedBy(username);

        filmService.save(film);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(HttpServletRequest request, @RequestBody FilmEntity film){
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return R.error("token为空");
        }

        String username = requireService.getLoginUser(token);

        film.setModifiedBy(username);
		filmService.updateById(film);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		filmService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/info/{name}")
    public R info(@PathVariable("name") String name) {
        String uuid = UUID.randomUUID().toString();

        if (map.containsKey(name)) {
            return R.error("用户 " + name + " 已被注册");
        }

        map.put(name, uuid);

        return R.ok().put("key", name).put("value", map.get(name));
    }
//    /**
//     * 测试openFeign设置请求头
//     * @param request
//     * @return
//     */
//    @GetMapping("/test")
//    public R test(HttpServletRequest request) {
//
//        String token = request.getHeader("token");
//        System.out.println("token");
//        JSONObject info = renrenFastFeign.info(token);
//        System.out.println(info.toString());
//        return R.ok().put("info", info);
//    }
}
